class StoreProduct < ActiveRecord::Base
  belongs_to :resource, :polymorphic => true
  
  belongs_to :print_product
  belongs_to :print_category
  belongs_to :store
  
  validates_presence_of :name, :message => "必须填写产品名称"
  validates_presence_of :price, :message => "必须填写产品价格"
  validates_numericality_of :price, :message => "产品价格的个是为10.00"
  validates_presence_of :duration, :message => "必须填写制作产品所耗时间"
  
  def self.create_store_product(params, store_id, print_category_id, print_product_id)
    store_product = StoreProduct.new
    
    store_product.store_id = store_id
    store_product.print_category_id = print_category_id
    store_product.print_product_id = print_product_id
    store_product.description = params[:description]
    store_product.price = params[:price]
    
    store_product
  end
  
  # 如果该属性本身没有，那么从resource中拿，resource是具体子类
  def get_prop_value(name)
    if respond_to? name
      eval(name)
    elsif @resource.respond_to? name
      eval("@resource.#{name}")
    end
  end
end
